<?php 
include_once '../../connection/connection_database.php';
include_once '../../language/vn.php';
include_once '../../language/url.php';
include_once '../../module/controls.php';

if(isset($_POST)){
    $c = new controls();
    $arrUrl = explode("/", $url["home"]);
    $url = "/".$arrUrl[1]."/".$arrUrl[2];
    
    $sql = "SELECT * FROM users WHERE user_name='{$_POST["username"]}'";
    $query = mysql_query($sql) or die(mysql_error());
    $fillByName = mysql_fetch_array($query);
    $level_decode = json_decode($fillByName["level"], true);
    
    $sql = "SELECT * FROM category c, cate_board cb WHERE c.category_id=cb.category_id and cb.category_id = '" . $_POST["id"] . "' ORDER BY board_order ASC ";
    $result = mysql_query($sql) or die(mysql_error());
}
?>
<form method="post" action="<?= $url ?>/menu/boardprocess/t/1">
    <p>
    <ul id="sortable">
        <?php
        while ($rows = mysql_fetch_array($result)) {
            ?>
            <li class="ui-state-default-cate"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>
                <?php
                $value = array("board_id" => $rows["board_id"], "order" => $rows["board_order"]);
                ?>
                <input type="hidden" value='<?php print_r(json_encode($value)) ?>' name="board_name[]" id="board_name[]"/>
                <span class="right">
                    <?php if ($level_decode["admin"] != "" || $level_decode["update"] != "") { ?>
                        <a href="<?= $url ?>/menu/boardupdate/t/1/id/<?php echo $rows["board_id"] ?>">
                            <image src="<?= $url ?>/images/icons/edit-icon.gif"/>
                        </a>
                    <?php } ?>
                    <?php if ($level_decode["admin"] != "" || $level_decode["update"] != "" && $level_decode["delete"]) { ?>
                        | 
                    <?php } ?>
                    <?php if ($level_decode["admin"] != "" || $level_decode["delete"] != "") { ?>
                        <a href="<?= $url ?>/menu/boarddelete/t/1/id/<?php echo $rows["board_id"] ?>" onclick="return confirm('Bạn có chắc chắn muốn xóa không.');">
                                <image src="<?= $url ?>/images/icons/b_drop.png"/>
                        </a>
                    <?php } ?>
                </span>
                <span class="center">
                    <?php echo $rows["board_name"] ?>
                </span>
            </li>
        <?php } ?>
        <?php if (mysql_num_rows($result) > 0) { ?>
            <?php echo $c->get_button("updateorder", "update") ?>
        <?php } ?>
    </ul>
</p>
</form>

<script>
    $(function() {
        $( "#sortable" ).sortable({
            start: function(event, ui) {
                var start_pos = ui.item.index();
                ui.item.data('start_pos', start_pos);
            },
            change: function(event, ui) {
                var start_pos = ui.item.data('start_pos');
                var index = ui.placeholder.index();
                if (start_pos < index) {
                    $('#sortable li:nth-child(' + index + ')').addClass('highlights');
                } else {
                    $('#sortable li:eq(' + (index + 1) + ')').addClass('highlights');
                }
            },
            update: function(event, ui) {
                $("li.ui-state-default-cate").each(function(index){
                    var oS = $.parseJSON($('li.ui-state-default-cate').eq(index).children('input').val());
                    var arrS = '{"board_id":"'+oS.board_id+'","order":"'+index+'"}';
                    $('li.ui-state-default-cate').eq(index).children('input').val(arrS);
                });
            }
        });
        $("#sortable").disableSelection(); 
    });
</script>